<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\Relation;
use app\common\tool\Result;
use think\App;
use think\facade\View;
use think\response\Json;
use addons\Builder\facade\Builder;
use app\common\model\Assets;
use app\common\model\Level;
use app\common\model\Logs;
use think\exception\ValidateException;

class User extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //验证器
    protected string $validate = 'User';

    //主模型
    protected string $model = '\app\common\model\User';


    protected array $sort = [
        'formal' => [
            'active_time' => 'desc'
        ],
    ];


    //正式会员
    public function formal(): string
    {
        return $this->setTable([], [], [['is_active', '=', 2]]);
    }

    public function setTable($rb = [], $tb = [], $extra_map = []): string
    {
        //构建表格数据
        $table = Builder::Table()
            ->addColumn(['type' => 'checkbox', 'title' => '全选', 'fixed' => 'left'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80, 'fixed' => 'left'])
            ->addColumn(['field' => 'nickname', 'title' => '会员昵称', 'modify' => '2,input'])
            // ->addColumn(['field' => 'code', 'title' => '会员编号', 'modify' => '3,input', 'fixed' => 'left'])
            ->addColumn(['field' => 'mobile', 'title' => '手机号', 'modify' => '2,input']);
       
        return $table
            ->addColumn(['field' => 'create_time', 'title' => '注册时间', 'width' => 300])
            // ->addColumn(['field' => 'active_time', 'title' => '激活时间', 'width' => 150])
            ->addColumn(['field' => 'is_lock', 'title' => '锁定登录', 'templet' => '#is_lock', 'width' => 120])
            ->addColumn(['field' => 'is_message', 'title' => '接收推送消息', 'templet' => '#is_message', 'width' => 120])
            // ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 150])
            ->addRightButtons(
                array_merge([
                ], $rb))
            ->addTopButtons(
                array_merge($tb, [
                    'export'
                ]))
            ->addSwitchTool([['name' => 'is_lock', 'pk' => $this->pk, 'text' => '锁定|正常'],['name' => 'is_message', 'pk' => $this->pk, 'text' => '是|否']])
            ->addExtraMap($extra_map)
            ->addJsUrl(['user'])
            ->addSearch([
                ['code__nickname__mobile', '会员编号/昵称/手机号', 'input,Like']
            ])
            ->fetch();

    }

    public function save($param = [], $model = true): Json
    {
        return parent::save($this->request->post(), $model); // TODO: Change the autogenerated stub
    }


    public function add($tpl = 'user/add')
    {
        return parent::add($tpl); // TODO: Change the autogenerated stub
    }

    public function edit($id = '', $tpl = 'user/edit', $info = '')
    {
        $team = \app\common\model\UserTeam::column('title,id');
        View::assign('team', $team);
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }


    public function delete($key = 'is_del'): Json
    {

        try {
            $id = $this->request->post($this->pk);
            $user = $this->model::where('id', $id)->find();
            if (empty($user)) {
                return json(['code' => -1, 'msg' => '会员不存在']);
            }
            if ($user->is_active == 2) {
                return json(['code' => -1, 'msg' => '会员已激活，无法删除']);
            }
            $count = Relation::where('uid', $user->id)->count();
            if ($count) {
                return json(['code' => -1, 'msg' => '该会员已推荐会员无法删除']);
            }
            Relation::destroy(['new_uid' => $user->id], true);
            $this->model::destroy(['id' => $user->id], true);

            Logs::adds('删除会员【' . $user->code . '】', $this->admin->id, $this->admin->admin_name);
            return json(['code' => 1, 'msg' => '已删除']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }


    }

}
